package com.dc.modules.sys.user.service;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.dc.commons.exception.DcBusinessException;
import com.dc.commons.persistence.embeddable.Attachment;
import com.dc.commons.query.DcQuery;
import com.dc.commons.query.ExcelQueryPageService;
import com.dc.commons.service.BaseService;
import com.dc.commons.status.UseStatus;
import com.dc.commons.utils.Excel;
import com.dc.commons.utils.excel.ExcelData;
import com.dc.commons.utils.excel.RowData;
import com.dc.modules.sys.user.domain.DcSysUser;
import com.dc.modules.sys.user.repository.DcSysUserRepository;
import com.google.common.collect.Lists;

/*******************************************************************************<P>
 * 文件名: DcSysUserService.java <br>
 * 版本: 1.0<br>
 * 描述: 用户表 管理service<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年4月7日 下午5:05:17<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
@Service
public class DcSysUserService extends BaseService<DcSysUser> implements ExcelQueryPageService<DcSysUser>{
	
	
	@Autowired
	private DcSysUserRepository dcSysUserRepository;
	
	/**
	 * 用户管理
	 * @param dcSysUser
	 * @return DcSysUser
	 */
	@Transactional(readOnly=true)
	public DcSysUser sysUserManager(DcSysUser dcSysUser){
		return dcSysUser;
	}
	
	/**
	 * 分页查询用户信息
	 * @param dcQuery
	 * @return Page-DcSysUser
	 */
	@Transactional(readOnly=true)
	public Page<DcSysUser> queryPage(DcQuery dcQuery){
		return dcSysUserRepository.findAll(dcQuery.buildSpecification(DcSysUser.class), dcQuery.getPageable());
	}
	
	/**
	 * 导入用户信息
	 * @param sysUser 将一些初始化的数值可以放在这里
	 * @param attachment
	 * @return DcSysUser
	 */
	public int importData(DcSysUser sysUser,Attachment attachment){
		File file=new File(attachment.getLink());
		ExcelData excelData= Excel.ImPortor.importData(file);
		
		final List<DcSysUser> users=Lists.newArrayList();
		for(RowData row:excelData.getRows()){
			 DcSysUser user=sysUser.convert(row);
			 // TODO 在此处添加 判断是否有效
			 users.add(user);
			if(users.size()==500){
				dcSysUserRepository.save(users);
				dcSysUserRepository.flush();
				users.clear();
			}
		}
		if(!users.isEmpty()){
			dcSysUserRepository.save(users);
		}
		
		return 0;
	}
	
	/**
	 * 进入到新增用户 页面前的业务处理
	 * @param dcSysUser
	 * @return DcSysUser
	 */
	@Transactional(readOnly=true)
	public DcSysUser sysUserAdd(DcSysUser dcSysUser){
		return dcSysUser;
	}
	
	/**
	 * 保存 用户信息
	 * @param dcSysUser
	 * @return DcSysUser
	 */
	public DcSysUser addSysUser(DcSysUser dcSysUser){
		return dcSysUserRepository.save(dcSysUser);
	}
	
	/**
	 * 进入 修改用户 页面前的业务处理
	 * @param dcSysUser
	 * @return DcSysUser
	 */
	@Transactional(readOnly=true)
	public DcSysUser sysUserEdit(DcSysUser dcSysUser){
		Assert.notNull(dcSysUser.getId(),"用户主键不能为空！");
		return dcSysUserRepository.findOne(dcSysUser.getId());
	}
	
	
	/**
	 * 修改 用户信息
	 * @param dcSysUser
	 * @return DcSysUser
	 */
	public DcSysUser editSysUser(DcSysUser dcSysUser){
		return dcSysUserRepository.save(dcSysUser);
	}
	
	/**
	 *  根据ID查找用户信息
	 * @param id
	 * @return DcSysUser
	 */
	public DcSysUser findSysUser(String id){
		return dcSysUserRepository.findOne(id);
	}
	
	/**
	 *  删除 用户  返回删除的用户信息
	 * @param dcSysUser
	 * @return DcSysUser
	 */
	public DcSysUser deleteSysUser(DcSysUser dcSysUser){
		
		dcSysUserRepository.remove(dcSysUser);
		return dcSysUser;
	}
	
	/**
	 * 根据ID删除
	 * @param ids
	 */
	public void deleteAllSysUser(String[] ids){
		List<Serializable> idList=Lists.newArrayList((Serializable[])ids);
		dcSysUserRepository.removeByIds(idList);
	}
	
	/**
	 *  启用 用户
	 * @param dcSysUser
	 */
	public void enabledSysUser(DcSysUser dcSysUser){
		
		if(dcSysUser.getUseStatus()==UseStatus.DE_ACTIVATE){
			throw new DcBusinessException(" 用户已经停用，不能 再次启用！");
		}
		if(dcSysUser.getUseStatus()==UseStatus.ENABLED){
			throw new DcBusinessException(" 用户已经启用，不需要再次启用！");
		}
		dcSysUserRepository.enabled(dcSysUser);
	}
	
	/**
	 *  暂停 用户
	 * @param dcSysUser
	 */
	public void suspendSysUser(DcSysUser dcSysUser){
		
		if(dcSysUser.getUseStatus()==UseStatus.DE_ACTIVATE){
			throw new DcBusinessException(" 用户已经停用，不能 再次启用！");
		}
		if(dcSysUser.getUseStatus()==UseStatus.SUSPEND){
			throw new DcBusinessException(" 用户已经暂停，不需要再次暂停！");
		}
		dcSysUserRepository.suspend(dcSysUser);
	}
	
	/**
	 *  停用 用户
	 * @param dcSysUser
	 */
	public void deActivateSysUser(DcSysUser dcSysUser){
		
		if(dcSysUser.getUseStatus()==UseStatus.DE_ACTIVATE){
			throw new DcBusinessException(" 用户已经停用，不需要再次停用！");
		}
		dcSysUserRepository.deActivate(dcSysUser);
	}
	
	
}
